/** Anastasia Karpinska, 2011 */

/*----------------------------------------------------------------------------*/

#include "datareader.h"
#include "fft/fft.h"

#include <fstream>

/*----------------------------------------------------------------------------*/


DataReader::DataReader()
{
}// DataReader::DataReader


/*----------------------------------------------------------------------------*/


void
DataReader::readData( const std::string& _fileName )
{
	m_statogramms.clear();
	m_amplitudes.clear();
	readStatogram( _fileName );

	const int inputs = 1024;
	const int values = 256;

	for ( size_t i = 0; i < m_statogramms.size(); i++ )
	{
		std::vector<complex> spectrum;
		spectrum.resize( inputs );
		for ( int j = 0; j < inputs; j++ )
			spectrum[j] = complex( m_statogramms[i][j].first, m_statogramms[i][j].second );

		if ( !CFFT::Forward( &spectrum[0], spectrum.size() ) )
			throw std::exception();

		for ( int i = 0; i < values; i++ )
			m_amplitudes.push_back( spectrum[i].norm() );
	}
} // DataReader::readData


/*----------------------------------------------------------------------------*/


const std::vector< float >&
DataReader::getAmplitudes() const
{
	return m_amplitudes;
} // DataReader::getAmplitudes


/*----------------------------------------------------------------------------*/


void
DataReader::readStatogram( const std::string& _fileName )
{
	std::ifstream input( _fileName.c_str() );

	for ( int i = 0; i < 1; i++ )
	{
		int pointsCount;
		input >> pointsCount;

		Statogram statogram;
		statogram.resize( pointsCount );
		for ( int i = 0; i < pointsCount; i++ )
		{
			double x, y;
			input >> x >> y;
			statogram[i] = std::make_pair( x, y );
		}

		m_statogramms.push_back( statogram );
	}
} // DataReader::readStatogram


/*----------------------------------------------------------------------------*/
